# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2010  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4 import QtCore
from PyQt4.QtGui import *

import sys
sys.path.append('../')
from aeDatabase import Pathway

from random import shuffle

from reactionVisual import ReactionVisual
from connection import Connection

colors = [QColor(255,000,000,50),
		  QColor(000,255,000,50),
		  QColor(000,255,255,50),
		  QColor(255,000,255,50),
		  QColor(255,255,000,50)]

class PathwayScene(QGraphicsScene):

	def __init__(self, parent=None):
		QGraphicsScene.__init__(self, parent)

		self.products = {}
		self.connections = []

		self.oldSelection = []

		self.connect(self, QtCore.SIGNAL("selectionChanged()"), self.newSelection)

	def addItem(self, item):
		if type(item) == ReactionVisual:
			# Add all of the products of this reaction to the list
			# so we can search it later (TODO do we need to update old things?)
			products = item.getProducts()
			for compound in products:
				try:
					self.products[compound].append(item)
				except KeyError:
					self.products[compound] = [item]

			# For each reactant find if corresponding product
			reactants = item.getReactants()
			for compound in reactants:
				try:
					reactions = self.products[compound]
				except KeyError:
					reactions = []

				# If there was a reaction found with same product then add connection
				for reaction in reactions:
					shuffle(colors)
					color = colors[0]
					for color in colors:
						if reaction.testColor(color) and item.testColor(color):
							break
					reaction.setCompoundColor(compound, color)
					item.setCompoundColor(compound, color)
					
					conn = Connection(reaction, item, compound, color)
					self.connections.append(conn)
					QGraphicsScene.addItem(self, conn)
		
		QGraphicsScene.addItem(self, item)


	def newSelection(self):
		for item in self.oldSelection:
			item.setSelected(False)
		self.oldSelection = self.selectedItems()
		for item in self.oldSelection:
			item.setSelected(True)

	def getPathway(self):
		pathway = Pathway()
		graph = {}
		for conn in self.connections:
			graph.update(conn.getSerial())
		pathway.graph = graph

		return pathway

	def removeItem(self, item):
		if type(item) == ReactionVisual:
			for conn in self.connections:
				if conn.productReaction == item.reaction or conn.reactantReaction == item.reaction:
					QGraphicsScene.removeItem(self, conn)
		QGraphicsScene.removeItem(self, item)